import { Controller, Delete, Get, Post, Param, Body } from "@nestjs/common";
import { DepartmentService } from "./department.service";
import { CreateDepartmentDto } from "@dto/department.dto";

@Controller('department')
export class DepartmentController {
    constructor(private readonly departmentService: DepartmentService) {
    }

    /**
     * 创建部门
     * @param {number} pid 父级id，顶级传入root
     * @param {CreateDepartmentDto} data
     */
    @Post('/:pid')
    createDept(@Param('pid') pid: string, @Body() data: CreateDepartmentDto) {
        return this.departmentService.createDept(pid === 'root' ? 0 : +pid, data)
    }

    /**
     * 获取部门列表
     */
    @Get()
    getDeptList() {
        return this.departmentService.getDeptList()
    }

    @Get('/list/:pid')
    getDeptListByParentId(@Param('pid') pid: string) {
        return this.departmentService.getDeptListByParentId(pid === 'root' ? 0 : +pid)
    }

    /**
     * 获取部门树
     */
    @Get('/tree')
    getDeptTree() {
        return this.departmentService.getDeptTree()
    }

    @Delete('/:id')
    deleteDeptById(@Param('id') id: string) {
        return this.departmentService.deleteDeptById(+id)
    }
}
